package com.huaxinkai.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.huaxinkai.entity.BaseEntity;
import com.huaxinkai.service.IBaseDao;

/**
 * 数据持久化基础操作类
 * @author liuzh
 * @data 2017年1月2日上午11:53:16
 */
@SuppressWarnings({"rawtypes"})
public class BaseDao implements IBaseDao {
	
	@Resource
	protected SessionFactory sessionFactory;
	private Session session;
	private Transaction tx;

	@Override
	public Session getSession() throws Exception {
		return session;
	}

	@Override
	public void getCurrentSession() throws Exception {
		session = sessionFactory.getCurrentSession();
		System.out.println("获取 --- session:"+session);
	}

	@Override
	public void begin() {
		tx = session.beginTransaction();
	}

	@Override
	public void commit() {
		tx.commit();
	}

	@Override
	public void openSession() throws Exception {
		session = sessionFactory.openSession();
	}

	@Override
	public void closeSession() throws Exception {
		if (null != session && session.isOpen()) {
			session.close();
		}
	}

	@Override
	public boolean insert(BaseEntity entity) throws Exception {
		session.save(entity);
		return true;
	}

	@Override
	public boolean delete(BaseEntity entity) throws Exception {
		session.delete(entity);
		return true;
	}

	@Override
	public boolean update(BaseEntity entity) throws Exception {
		session.update(entity);
		return true;
	}

	@Override
	public Object selectOne(String sql) throws Exception {
		return session.createQuery(sql).uniqueResult();
	}

	@Override
	public Object selectOne(Class clazz, int id) throws Exception {
		return session.get(clazz, id);
	}

	@Override
	public Object selectOne(Class clazz, String column, Object value) throws Exception {
		String hql = String.format("FROM %s WHERE " + column + "=" + value, clazz.getSimpleName());
		return session.createQuery(hql).uniqueResult();
	}

	@Override
	public List query(Class clazz, String column, Object value) throws Exception {
		String hql = String.format("FROM %s WHERE " + column + "=" + value, clazz.getSimpleName());
		return session.createQuery(hql).list();
	}

	@Override
	public List query(Class clazz, String[] column, Object[] value) throws Exception {
		String hql = String.format("FROM %s WHERE 1 = 1 ", clazz.getSimpleName());
		for (int i = 0; i < column.length; i++) {
			hql += " AND " + column[i] + "=" + value[i];
		}
		return session.createQuery(hql).list();
	}

	@Override
	public Object selectOne(String hql, Map<String, Object> params) throws Exception {
		return session.createQuery(hql).setProperties(params).uniqueResult();
	}

	@Override
	public List select(String hql) throws Exception {
		return session.createQuery(hql).list();
	}
	@Override
	public List select(Class clazz) throws Exception {
		return session.createQuery("from " + clazz.getSimpleName()).list();
	}
	@Override
	public List select(Class clazz, Integer... ids) throws Exception {
		return session.createQuery("from " + clazz.getSimpleName() + " where id in(:ids) ").setParameterList("ids", ids).list();
	}
	@Override
	public List select(Class clazz, int page, int rows) throws Exception {
		return session.createQuery("from " + clazz.getSimpleName()).setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public List select(String hql, Map<String, Object> params) throws Exception {
		return session.createQuery(hql).setProperties(params).list();
	}
	
	@Override
	public List select(String hql, Map<String, Object> params, int page, int rows) throws Exception {
		return session.createQuery(hql).setProperties(params).setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}
	
	@Override
	public long selectCount(Class clazz) throws Exception {
		return (Long) session.createQuery("select COUNT(*) from " + clazz.getSimpleName()).uniqueResult();
	}

	@Override
	public long selectCount(String hql, Map<String, Object> params) throws Exception {
		return (Long) session.createQuery(hql).setProperties(params).uniqueResult();
	}

	@Override
	public boolean delete(List<BaseEntity> entities) throws Exception {
		for (Object entity : entities) {
			session.delete(entity);
		}
		return true;
	}

	@Override
	public boolean insert(List<BaseEntity> entities) throws Exception {
		for (Object entity : entities) {
			session.save(entity);
		}
		return true;
	}

}